05. Select Where

Select Where

Question:

Start Quiz:

# The query below finds the names and birthdates of all the gorillas.
# 
# Modify it to make it find the names of all the animals that are not
# gorillas and not named 'Max'.
#

QUERY = '''
select name, birthdate from animals where species = 'gorilla';
'''

User's Answer:

(Note: The answer done by the user is not guaranteed to be correct)

# The query below finds the names and birthdates of all the gorillas.
# 
# Modify it to make it find the names of all the animals that are not
# gorillas and not named 'Max'.
#

QUERY = '''
select name from animals where species != 'gorilla' and name != 'Max';
'''
Solution:

INSTRUCTOR NOTE:

The syntax of the select statement with a where clause:

select columns from tables where condition ;

Columns are separated by commas; use * to select all columns.

The condition is a Boolean expression on column values. SQL supports the Boolean operations and, or, and not which work the same as in Python.

We can switch between the expression form (not X) and (not Y) and the form not (X or Y) because of a logic rule called DeMorgan's Law. You can read more about it in its Wikipedia article.